package 学生;

import GUI.*;
import 数据库和网络.myDatebase;

import java.sql.SQLException;

public class Student {
    private String name,schoolID,password,myClass;
    String []teacher;
    int instantScore;

    public Student(int score){
        instantScore=score;
    }

    public Student(String name){
        this.schoolID=name;
    }

    public Student(){}

    public Student(String name,String id,String clas){
        this.name=name;
        this.schoolID=id;
        this.myClass=clas;
    }

    public void studentLog(String ID,String password){
        myDatebase LogConfirm = new myDatebase("student");

        String sql = "select StudentName,StudentID,StudentPassWord,class from student.student ";
        try {
            LogConfirm.statement.execute(sql);
            LogConfirm.resultSet = LogConfirm.statement.executeQuery(sql);
            boolean isRight = false;
            while (LogConfirm.resultSet.next()) {
                if (ID.equals(LogConfirm.resultSet.getString(2)) && password.equals(LogConfirm.resultSet.getString(3))) {
                    isRight=true;
                    enterWindow.currentStudent.setName(LogConfirm.resultSet.getString(1));
                    enterWindow.currentStudent.setSchoolID(ID);
                    enterWindow.currentStudent.setMyClass(LogConfirm.resultSet.getString(4));/*在学生类的方法中为enterWindow中的
                    currentStudent设定ID和班级，便于查找试卷之类的操作*/

                    new studentWindow("");
                    new successfulLog("a");
                }
            }
            if (!isRight) {

                new enterWindow();
                new incorrectLog();

            }
            LogConfirm.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

    public static void studentRegister(String preID,String prePassword){
        enterWindow.identity="学生";
        boolean isNew=true;
        myDatebase registerConfirm=new myDatebase("student");
        String sql= "select StudentID,StudentPassWord from student.student";
        try {
            registerConfirm.statement.execute(sql);
            registerConfirm.resultSet=registerConfirm.statement.executeQuery(sql);
            while(registerConfirm.resultSet.next()){
                if(preID.equals(registerConfirm.resultSet.getString(1)))
                {
                    isNew=false;
                    new incorrectRegister();
                    break;
                }
            }
            if(isNew){
                sql="insert student.student(studentid, studentpassword) values ('"+preID+"','"+prePassword+"')";
                registerConfirm.statement.execute(sql);
                new successfulRegister();
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }


    void finishPaper(){}

    void browseGrades(){}

    public String getName(){
        return name;
    }

    public String getSchoolID(){
        return schoolID;
    }

    public String getPassword(){
        return password;
    }

    public String getMyClass(){
        return myClass;
    }

    public String[] getTeacher(){
        return teacher;
    }

    public int getInstantScore(){
        return instantScore;
    }

    public void setInstantScore(int a){
        this.instantScore=a;
    }

    public String setName(String a){
        name=a;
        return name;
    }

    public String setSchoolID(String a){
        schoolID=a;
        return schoolID;
    }


    public String setPassword(String a){
        password=a;
        return password;
    }

    public String setMyClass(String a){
        myClass=a;
        return myClass;
    }

}
